﻿using System;
using System.Web.UI;
using SouthernLand.Common;
using SouthernLand.Common.BOL.Articles;
using SouthernLand.Common.Untilities;

namespace SouthernLand.WebMaster.Articles
{
    public partial class ArticleAddEdit : Page
    {
        private int ArticleID = -1;
        private int CateID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            ArticleID = Globals.GetIntFromQueryString("ArticleID");
            CateID = Globals.GetIntFromQueryString("CateID");
            if (Request.QueryString["Action"] == "Edit")
            {
                btSave.CommandArgument = "Edit";
                btSave.Text = " Update ";

                if (!IsPostBack)
                {
                    Load_GetItemByArticleID(ArticleID);
                }
            }
        }

        protected void Load_GetItemByArticleID(int ArticleID)
        {
            Article obj = Article.GetArticleByArticleID(ArticleID);
            if (obj != null)
            {
                txtTitle.Text = obj.Title;
                txtExcerpt.Text = obj.Excerpt;
                txtBody.Text = obj.Body;
                if (obj.Priority == 1)
                    chkPriority.Checked = true;

                if (obj.ImageURL.Length > 3)
                    ImageItem.Src = Globals.ApplicationPath + "UserUpload/Articles/" + obj.ImageURL;

                if (obj.ImageWidth > obj.ImageHeight)
                    ImageItem.Width = 80;
                else
                    ImageItem.Height = 80;

                dlArticleCategory.SelectedValue = obj.ArticleCateID.ToString();

                HiddenImageName.Value = obj.ImageURL;
                HiddenImageWidth.Value = obj.ImageWidth.ToString();
                HiddenImageHeight.Value = obj.ImageHeight.ToString();
            }
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            var obj = new Article();
            obj.ArticleID = ArticleID;
            obj.Title = txtTitle.Text.Trim();
            obj.Excerpt = txtExcerpt.Text.Trim().Replace(Environment.NewLine, "<br>");
            obj.Body = txtBody.Text;
            obj.PostDate = DateTime.Now;
            obj.Priority = 0;
            if (chkPriority.Checked)
                obj.Priority = 1;
            obj.ImageURL = HiddenImageName.Value.Trim();
            obj.ImageWidth = ConvertUtility.ToInt32(HiddenImageWidth.Value.Trim());
            obj.ImageHeight = ConvertUtility.ToInt32(HiddenImageHeight.Value.Trim());
            obj.Authors = " ";
            obj.ArticleCateID = ConvertUtility.ToInt32(dlArticleCategory.SelectedItem.Value);

            if (btSave.CommandArgument == "Edit")
            {
                obj.TotalViews = 0;
                Article.Update(obj);
            }
            else
            {
                Article.Insert(obj);
            }
            lblMsg.Text = "Cập nhật thông tin thành công!";
            Response.Redirect("Default.aspx?CateID=" + CateID);
        }
    }
}